package cn.iocoder.yudao.module.system.controller.admin.oauth2;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.Oauth2ClientCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.Oauth2ClientPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.Oauth2ClientRespVO;
import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.Oauth2ClientUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.auth.Oauth2ClientConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.Oauth2ClientDO;
import cn.iocoder.yudao.module.system.service.oauth2.Oauth2ClientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - OAuth2 客户端
 *
 * @author huangweidong
 * @date 2023/05/09
 */
@Tag(name = "管理后台 - OAuth2 客户端")
@RestController
@RequestMapping("/system/oauth2-client")
@Validated
public class Oauth2ClientController {

    @Resource
    private Oauth2ClientService oAuth2ClientService;

    @PostMapping("/create")
    @Operation(summary = "创建 OAuth2 客户端")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:create')")
    public CommonResult<Long> createOauth2Client(@Valid @RequestBody Oauth2ClientCreateReqVO createReqVO) {
        return success(oAuth2ClientService.createOauth2Client(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 OAuth2 客户端")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:update')")
    public CommonResult<Boolean> updateOauth2Client(@Valid @RequestBody Oauth2ClientUpdateReqVO updateReqVO) {
        oAuth2ClientService.updateOauth2Client(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 OAuth2 客户端")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:delete')")
    public CommonResult<Boolean> deleteOauth2Client(@RequestParam("id") Long id) {
        oAuth2ClientService.deleteOauth2Client(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 OAuth2 客户端")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:query')")
    public CommonResult<Oauth2ClientRespVO> getOauth2Client(@RequestParam("id") Long id) {
        Oauth2ClientDO oAuth2Client = oAuth2ClientService.getOauth2Client(id);
        return success(Oauth2ClientConvert.INSTANCE.convert(oAuth2Client));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OAuth2 客户端分页")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:query')")
    public CommonResult<PageResult<Oauth2ClientRespVO>> getOauth2ClientPage(@Valid Oauth2ClientPageReqVO pageVO) {
        PageResult<Oauth2ClientDO> pageResult = oAuth2ClientService.getOauth2ClientPage(pageVO);
        return success(Oauth2ClientConvert.INSTANCE.convertPage(pageResult));
    }

}
